/**
 *  Copyright(C) 2012-2013 Suntec(Shanghai) Software Co., Ltd.
 *  All Right Reserved.
 */
package com.cugbmao.lucene;

import java.io.File;
import java.io.IOException;

import junit.framework.TestCase;

import org.apache.lucene.analysis.core.SimpleAnalyzer;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

import com.cugbmao.lucene.util.TestUtil;

/**
 * Descriptions
 * 
 * @version 2013年5月27日
 * @author Suntec
 * @since JDK1.6
 * 
 */
public class LockTest extends TestCase {
	private Directory dir;
	private File indexDir;

	/*
	 * (non-Javadoc)
	 * @see junit.framework.TestCase#setUp()
	 */
	@Override
	protected void setUp() throws Exception {
		indexDir = new File( System.getProperty( "java.io.tmpdir", "tmp" ) + System.getProperty( "file.separator" )
				+ "index" );
		dir = FSDirectory.open( indexDir );
	}

	public void testWriteLock() throws IOException {
		IndexWriterConfig iwf = new IndexWriterConfig( Version.LUCENE_43, new SimpleAnalyzer( Version.LUCENE_43 ) );
		IndexWriter writer1 = new IndexWriter( dir, iwf );
		IndexWriter writer2 = null;
		try {
			writer2 = new IndexWriter( dir, iwf );
			fail( "We should never reach this point" );
		}
		catch ( Exception e ) {
			e.printStackTrace();
		}
		finally {
			writer1.close();
			assertNull( writer2 );
			TestUtil.rmDir( indexDir );
		}
	}
}
